package org.tinygroup.template.function.escape;

import org.apache.commons.lang.StringUtils;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.template.Template;
import org.tinygroup.template.TemplateContext;
import org.tinygroup.template.TemplateException;
import org.tinygroup.template.function.AbstractTemplateFunction;

/**
 * 抽象的转义/反转义基类
 * @author yancheng11334
 *
 */
public abstract class AbstractEscapeFunction extends AbstractTemplateFunction{

	protected String[] searchWords;
	protected String[] replaceWords;
	
	public String[] getSearchWords() {
		return searchWords;
	}

	public void setSearchWords(String[] searchWords) {
		this.searchWords = searchWords;
	}

	public String[] getReplaceWords() {
		return replaceWords;
	}

	public void setReplaceWords(String[] replaceWords) {
		this.replaceWords = replaceWords;
	}

	public AbstractEscapeFunction(String names) {
		super(names);
	}

	public Object execute(Template template, TemplateContext context, Object... parameters) throws TemplateException{
		if(parameters==null || parameters.length<1){
		   throw new TemplateException(String.format("函数%s参数格式不正确.", getNames()));
		}
		if(parameters[0]==null){
		   return null; //直接返回
		}
		String content = parameters[0].toString();
		if(StringUtil.isEmpty(content)){
		   return content;
		}
		return replace(content);
	}
	
	
	/**
	 * 动态替换
	 * @param content
	 * @return
	 */
	protected String replace(String content) throws TemplateException{
		try{
			return StringUtils.replaceEach(content, searchWords, replaceWords);
		}catch(Exception e){
			throw new TemplateException(e);
		}
		
	}
	
}
